Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Semigroup.instance method #2101

Merged
merged 1 commit into from
Dec 14, 2017
Merged

Conversation

jozic
Copy link
Contributor

@jozic jozic commented Dec 13, 2017

No description provided.

@codecov-io
Copy link

codecov-io commented Dec 13, 2017

Codecov Report

Merging #2101 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2101      +/-   ##
==========================================
+ Coverage   94.75%   94.75%   +<.01%     
==========================================
  Files         322      322              
  Lines        5454     5456       +2     
  Branches      214      224      +10     
==========================================
+ Hits         5168     5170       +2     
  Misses        286      286
Impacted Files Coverage Δ
kernel/src/main/scala/cats/kernel/Semigroup.scala 68.75% <100%> (+4.46%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e27266a...f126e29. Read the comment docs.

kailuowang
kailuowang previously approved these changes Dec 13, 2017
Copy link
Contributor

@kailuowang kailuowang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.
As a quick side note, with LAMBDA SYNTAX FOR SAM TYPES in Scala 2.12, you can create a new instance even more succinctly as

def algebra[A]: Semigroup[F[A]] = self.combineK

Copy link
Contributor

@ceedubs ceedubs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @jozic!

I do think that the instance method is convenient if there are people stuck on Java versions in which they can't use the SAM syntax that @kailuowang mentioned.

I don't know whether it would be significant enough to worry about, but I do wonder about slight performance implications of the Semigroup definitions for Either, Validated, etc using Semigroup.instance as opposed to what they were using before.

Please see my comment about the unrelated change in the pure implementation for Semigroup.

if (as.isEmpty) None
else if (as.size == 1) as.toList.headOption
else Some(a)
if (as.size > 1) Some(a) else as.toTraversable.headOption
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There may be merit to these changes, but they seem to be unrelated to the rest of the PR, and I think that they should be separated out into another PR. I'm not sure if there are types that implement TraversableOnce for which an isEmpty check would be significantly faster than a size check on an empty collection, but it's something that people might not think about when reviewing this PR.

Copy link
Contributor Author

@jozic jozic Dec 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Np, I will revert these changes, they are indeed not related to Semigroup.instance

should I open a new PR for it?

my first assumption here was that toTraversable convertion instead of toList can be cheaper/faster
second one is that headOption already does same check as removed line 22

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kailuowang that's true, but unfortunately not all of us are in Java 8/Scala 2.12 lands yet

@ceedubs
I can write jmh benchmarks for both cases - the old way of creating instances and the new one using Semigroup.instance
OR
alternatively I can revert those changes and just leave addition of Semigroup.instance , as this is the main goal here. Then we can try another PR with benchmarks or just let it go
Please let me know

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can revert those changes and just leave addition of Semigroup.instance , as this is the main goal here. Then we can try another PR with benchmarks or just let it go

@jozic this sounds like the path of least resistance to me. Just please be sure to add a test for the Semigroup.instance method, since I think that currently it's only indirectly tested through the Semigroup instances that you are using it to create.

@jozic
Copy link
Contributor Author

jozic commented Dec 13, 2017

@kailuowang @ceedubs
thanks for your review!
I just pushed updates to the PR as discussed

2 questions left:

  1. Should I send PR with changes to catsInvariantMonoidalSemigroup.pure I excluded from this PR as they were irrelevant?
  2. Should I benchmark creation of default Semigroup instances w/ and w/o usage of Semigroup.instance introduced in this PR?

@johnynek
Copy link
Contributor

👍

@kailuowang
Copy link
Contributor

@jozic thanks! IRT InvarianyMonoidal change, do you mind take a look at #2088?
I would say just switching back to detail creation method inside cats might be easier than benchmarking... This kind of trait instance creation is all over cats. People are so used to read it that there isn't much gain in readability in replacing them with the convenience method, IMHO.

Copy link
Contributor

@ceedubs ceedubs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. Thanks again, @jozic!

I agree with @kailuowang's comment on the questions that you asked. I don't personally see much benefit in the change to the pure implementation, but I don't have strong feelings on it.

@ceedubs
Copy link
Contributor

ceedubs commented Dec 14, 2017

@kailuowang are you happy with this one? If so, feel free to merge.

@kailuowang kailuowang merged commit 51635f8 into typelevel:master Dec 14, 2017
@jozic jozic deleted the semigroup-instance branch December 14, 2017 03:30
@kailuowang kailuowang added this to the 1.0.0 milestone Dec 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants